package org.exeed.controller;

import org.exeed.aop.tag.Auth;
import org.exeed.bean.ResultBean;
import org.exeed.entity.ModEntity;
import org.exeed.entity.PageSketchEntity;
import org.exeed.service.ModService;
import org.exeed.service.PageService;
import org.exeed.service.PageSketchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//@CrossOrigin
@RestController
@RequestMapping("page")
public class PageController {

    @Autowired
    private PageSketchService pageSketchService;

    @Autowired
    private PageService pageService;

    @Auth
    @PostMapping("/saveSketch")
    public ResultBean saveSketch(@RequestBody PageSketchEntity entity) {

        if (pageSketchService.save(entity)) {
            return ResultBean.succeed();
        }

        return ResultBean.defeated("Home页面已创建.");
    }

    @Auth
    @GetMapping("/getSketchHomePage")
    public ResultBean getSketchHomePage() {
        return ResultBean.succeed(pageSketchService.selectOneByType("home"));
    }

    @GetMapping("/getHomePage")
    public ResultBean getHomePage() {
        return ResultBean.succeed(pageService.selectOneByType("home"));
    }

    @Auth
    @GetMapping("/selectSketchPages")
    public ResultBean selectSketchPages(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("key") String key
            , @RequestParam(value = "type", required = false) String type, @RequestParam(value = "isPublish", required = false) Integer isPublish) {
        return ResultBean.succeed(pageSketchService.selectList(page, size, key, type, isPublish));
    }

//    @Auth
    @GetMapping("/selectPages")
    public ResultBean selectPages(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("key") String key
            , @RequestParam(value = "type", required = false) String type) {
        return ResultBean.succeed(pageService.selectList(page, size, key, type, null));
    }

    @Auth
    @GetMapping("/getSketchById")
    public ResultBean getPage(@RequestParam("id") Long id) {
        return ResultBean.succeed(pageSketchService.selectById(id));
    }

    @GetMapping("/getPageById")
    public ResultBean getById(@RequestParam("id") Long id) {
        return ResultBean.succeed(pageService.selectById(id));
    }

//    @GetMapping("/selectPublishArticlePages")
//    public ResultBean selectPublishArticlePages(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("key") String key) {
//        return ResultBean.succeed(pageSketchService.selectList(page, size, key, "article", 1));
//    }
//
//    @GetMapping("/selectAllArticlePages")
//    public ResultBean selectArticlePages(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("key") String key) {
//        return ResultBean.succeed(pageSketchService.selectArticle(page, size, key, "article", null));
//    }
//
//    @GetMapping("/selectPublishMaininfoPages")
//    public ResultBean selectPublishMaininfoPages(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestParam("key") String key) {
//        return ResultBean.succeed(pageSketchService.selectArticle(page, size, key, 1));
//    }

    @Auth
    @PostMapping("/publish")
    public ResultBean publish(@RequestBody PageSketchEntity entity) {
        entity.setIsPublish(1);
        pageService.publish(entity);
        pageSketchService.save(entity);
        return ResultBean.succeed();
    }

    @Auth
    @PostMapping("/unpublish")
    public ResultBean unpublish(@RequestBody PageSketchEntity entity) {
        entity.setIsPublish(0);
        pageService.unpublish(entity);
        pageSketchService.save(entity);
        return ResultBean.succeed();
    }

    @Auth
    @GetMapping("/delete")
    public ResultBean delete(@RequestParam("id") Long id) {

        if (pageSketchService.delete(id)) {
            return ResultBean.succeed();
        }

        return ResultBean.defeated("删除已发布网页请先下架!");
    }

    @Auth
    @GetMapping("/check")
    public ResultBean checkExeed(@RequestParam("id") Long id) {

        return ResultBean.succeed();
    }


}
